<!--
 * @Description: 单据编码规则
 * @Author: 
 * @Date: 2020-06-29 11:18:28
 * @LastEditors: wangwangwang
 * @LastEditTime: 2021-03-18 10:11:03
-->
<template>
  <div class="pages_content table CodingRules">
    <el-container>
      <el-header>
        <breaDcrumb />
      </el-header>
      <el-main style="display: flex;">
        <div class="main_left">
          <treeContainer ref="treeRef" :arr="systemList" @chooseTree="setTree" />
        </div>
        <div class="main_right">
          <div class="header_right" @click="showCardNav = !showCardNav">
            <div class="title">搜索</div>
            <div>
              <transition name="from"></transition>
              <span v-show="!showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-down"></i>
              </span>
              <transition name="to"></transition>
              <span v-show="showCardNav">
                <i style="margin-right: 5px;" class="el-icon-arrow-up"></i>
              </span>
            </div>
          </div>
          <div v-show="showCardNav" class="hr"></div>
          <el-collapse-transition>
            <div v-show="showCardNav">
              <div class="search">
                <span style="display: inline-block;margin-bottom: 10px;">
                  <span style="margin-right: 12px;">名称</span>
                  <el-input v-model="tableParam.name" style="width: 200px;" clearable placeholder="请输入" />
                </span>
                <span style="margin-left: 24px;">
                  <span style="margin-right: 12px;">编码</span>
                  <el-input v-model="tableParam.code" style="width: 200px;" clearable placeholder="请输入" />
                </span>
                <el-button icon="el-icon-search" size="medium" class="bottom_all" @click="(tableParam.page = 1), initData()">查 询</el-button>
                <el-button size="medium" class="bottom_all_two" @click="reset()">重 置</el-button>
              </div>
            </div>
          </el-collapse-transition>
          <div class="hr"></div>
          <div class="card">
            <div class="card_nav">
              <div>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='TIANJIA'))!==-1" size="small" class="bottom_all" style="margin-left: 0;" @click="add()">
                  <i class="icon iconfont iconicon_button_add" style="font-size: 12px;"></i>
                  添加
                </el-button>
                <el-button v-if="ButtonList.findIndex(res=>(res.buttonValue=='SHANCHU'))!==-1" size="small" class="bottom_two" @click="batchUpdateOrDelete('update_status', 0)">
                  <i class="icon iconfont iconicon_button_delete" style="font-size: 12px;"></i>
                  删除
                </el-button>
              </div>
              <div>
                <el-dropdown :hide-on-click="false" trigger="click" style="margin: 0 20px;">
                  <el-link :underline="false">
                    <span class="el-icon--left">
                      <i class="iconfont iconicon_button_filtrate"></i>
                    </span>
                    筛选
                  </el-link>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item v-for="(item, index) in table" :key="index">
                      <el-checkbox v-model="item.status">{{ item.label }}</el-checkbox>
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
                <el-link v-if="ButtonList.findIndex(res=>(res.buttonValue=='DAOCHU'))!==-1" :underline="false" @click="$Download(DownloadURL, tableParam)">
                  <span class="el-icon--left">
                    <i class="iconfont iconicon_button_export1"></i>
                  </span>
                  导出
                </el-link>
              </div>
            </div>
            <!-- 列表 -->
            <div class="card_info" :style="style">
              <el-table ref="multipleTable" :header-cell-style="{ background: '#f3f6f9' }" :cell-style="{ borderRight: 'none' }" border stripe style="width:100%" :data="tableData" tooltip-effect="dark" @selection-change="handleSelectionChange">
                <el-table-column align="center" type="selection" width="50"></el-table-column>
                <template v-for="(item, index) in table">
                  <el-table-column v-if="item.status" :key="index" :label="item.label" align="center" :width="item.width">
                    <template slot-scope="scope">
                      <el-button v-if="item.label == '操作'&&ButtonList.findIndex(res=>(res.buttonValue=='XIUGAI'))!==-1" class="bottom_one" @click="update(scope.row)">
                        <i class="icon iconfont iconicon_button_modification" style="font-size: 12px;"></i>
                        修改
                      </el-button>
                      <span v-if="item.label !== '操作'">{{ scope.row[item.prop] }}</span>
                    </template>
                  </el-table-column>
                </template>
              </el-table>
            </div>
            <div class="card_bottom">
              <div class="card_bottom_left">
                <div class="top_page_totle">总共{{ total }}条</div>
                <el-pagination :current-page.sync="tableParam.page" :page-size="tableParam.pageSize" layout="prev, pager, next, jumper" :total="total" @current-change="initData()"></el-pagination>
              </div>
              <div>
                <el-button size="mini" icon="el-icon-refresh-right" class="bottom_all_two" @click="initData()">刷新</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-main>
    </el-container>
    <!-- 添加 -->
    <el-dialog v-dialogDrag :visible.sync="dialogVisible" width="620px" :before-close="resefrom">
      <span slot="title" class="dialog-title">{{ dialogtitle }}</span>
      <div class="dialoginfo">
        <el-form ref="dialogForm" :rules="rules" :inline="false" :model="from" label-width="90px">
          <el-form-item prop="code">
            <span slot="label" style="color:  #FF0000 ;">编码</span>
            <el-input v-model="from.code" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="name">
            <span slot="label" style="color:  #FF0000 ;">名称</span>
            <el-input v-model="from.name" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item prop="rules">
            <span slot="label">编码规则</span>
            <el-input v-model="from.rules" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <!-- <el-form-item prop="ruleTypeId">
            <span slot="label">单据类型</span>
            <el-input disabled v-model="from.ruleTypeName" placeholder="请输入" clearable></el-input>
          </el-form-item> -->
          <el-form-item prop="currentValue">
            <span slot="label">当前值</span>
            <el-input v-model="from.currentValue" placeholder="请输入" clearable></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button class="bottom_all_two" @click="resefrom()">取 消</el-button>
        <el-button class="bottom_all" @click="save()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import breaDcrumb from "@/components/breadcrumb";
import treeContainer from "@/components/GlobalTree/tree";
import { mapGetters } from "vuex";
import {
  queryPage,
  saveOrUpdate,
  getDetail,
  batchUpdateOrDelete, Download,
} from "@/api/basedata/CodingRules.js";
import { queryPage as getSystemList } from "@/api/basedata/business.js";
export default {
  components: {
    breaDcrumb,
    treeContainer,
  },
  data() {
    return {
      DownloadURL: Download,
      options: [],
      /*添加修改弹窗*/
      dialogVisible: false,
      dialogtitle: "添加编码规则",
      /*弹要修改过着添加的数据*/
      from: {
        name: "",
        code: "",
        currentValue: "",
        rules: "",
        ruleTypeId: "",
        ruleTypeName: "",
      },
      /*判断收起展开*/
      showCardNav: true,
      table: [
        { label: "编码", width: "auto", prop: "code", status: true },
        { label: "名称", width: "auto", prop: "name", status: true },
        { label: "编码规则", width: "auto", prop: "rules", status: true },
        {
          label: "所属系统",
          width: "auto",
          prop: "ruleTypeName",
          status: true,
        },
        { label: "当前值", width: "auto", prop: "currentValue", status: true },
        {
          label: "最后更新日期",
          width: "200",
          prop: "updateTime",
          status: true,
        },
        { label: "操作", width: "120", prop: "", status: true },
      ],
      /*列表选中的数据集*/
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入编码", trigger: "blur" }],
        currentValue: [
          { required: true, message: "请输入当前值", trigger: "blur" },
        ],
        rules: [{ required: true, message: "请输入编码类型", trigger: "blur" }],
      },
      tableParam: {
        page: 1,
        pageSize: 10,
        code: "",
        name: "",
        ruleTypeId: "",
      },
      total: 0,
      ids: [],
      sortIds: [],
      tableData: [],
      systemList: [],
      ButtonList: []
    };
  },
  mounted() {
    this.ButtonList = this.$ButtonList()
    this.initSystem();
    this.initData();
  },
  methods: {
    /*重置*/
    reset() {
      this.tableParam.code = "";
      this.tableParam.name = "";
      this.tableParam.ruleTypeId = "";
      this.initData();
      this.$refs.treeRef.clearSelect();
    },
    /*删除，禁用，启用，排序*/
    batchUpdateOrDelete(type, status) {
      if (this.ids.length == 0) {
        this.$message({
          message: "请选择要操作的数据项",
          type: "warning",
        });
        return;
      }
      if (type === "update_status") {
        this.$confirm("此操作将永久删除该信息?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          cancelButtonClass: "bottom_all_two",
          confirmButtonClass: "bottom_all",
          type: "warning",
        })
          .then(() => {
            batchUpdateOrDelete({
              ids: this.ids,
              status: status,
              type: type,
            }).then(() => {
              this.reset();
            });
          })
          .catch(() => { });
      } else {
        batchUpdateOrDelete({
          ids: this.ids,
          status: status,
          type: type,
        }).then(() => {
          this.initData();
        });
      }
    },
    /*接受树状数据*/
    setTree(value) {
      this.from.ruleTypeId = value.id;
      this.from.ruleTypeName = value.name;
      this.tableParam.ruleTypeId = value.id;
      this.initData();
    },
    /*添加*/
    add() {
      if (!this.from.ruleTypeId) {
        this.$message({
          message: "请先选择要新增的系统",
          type: "warning",
        });
        return;
      }
      this.dialogVisible = true;
      this.from.name = '';
      this.from.code = '';
      this.from.currentValue = '';
      this.from.rules = '';
      this.dialogtitle = "添加编码规则";
    },
    /*修改*/
    update(data) {
      getDetail({ id: data.id }).then((res) => {
        this.from = res.data;
        this.from.deleteLimit = JSON.stringify(res.data.deleteLimit);
        this.from.downloadLimit = JSON.stringify(res.data.downloadLimit);
        this.from.watermark = JSON.stringify(res.data.watermark);
        this.dialogtitle = "修改编码规则";
        this.dialogVisible = true;
      });
    },
    /**
     * @Description: 获取菜单列表和单据数据
     * @Param:
     * @Author: wangwangwang
     */
    initSystem() {
      getSystemList({ page: 1, pageSize: 1000 }).then((res) => {
        if (res.code === 1001) {
          this.systemList = res.data.data;
        }
      });
    },
    initData() {
      queryPage(this.tableParam).then((res) => {
        this.total = res.data.totalRows;
        this.tableData = res.data.data;
      });
    },
    /*保存或修改*/
    save() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          saveOrUpdate(this.from).then((res) => {
            if (res.code === 1001) {
              this.$message({
                type: "success",
                message: "操作成功",
              });
              this.resefrom();
              this.initData();
            }
          });
        }
      });
    },
    resefrom() {
      this.dialogVisible = false;
    },
    /*表头复选框*/
    toggleSelection(rows) {
      if (rows) {
        rows.forEach((row) => {
          this.$refs.multipleTable.toggleRowSelection(row);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.ids = [];
      this.sortIds = [];
      val.forEach((item) => {
        this.ids.push(item.id);
        this.sortIds.push({ id: item.id, sortNum: item.sortNum });
      });
    },
  },
  computed: {
    ...mapGetters({
      screenWidth: "getscreenWidth",
      screenHeight: "getscreenHeight",
      Collapse: "getsCollapse",
    }),
    style() {
      let num = 0;
      if (this.Collapse) {
        num = this.screenWidth - 324;
      } else {
        num = this.screenWidth - 421;
      }
      return "width:" + num + "px;";
    },
  },
};
</script>
<style lang="scss" scoped>
.CodingRules {
  .dialoginfo {
    .el-input,
    .el-select {
      width: 450px;
    }
  }
}
</style>
